package com.ruoyi.bizsys.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.bizsys.utils.CrsUserExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bizsys.domain.CrsUser;
import com.ruoyi.bizsys.service.ICrsUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * CRSController
 *
 * @author ygenwen
 * @date 2022-08-11
 */
@RestController
@RequestMapping("/bizsys/crsUser")
public class CrsUserController extends BaseController {
    @Autowired
    private ICrsUserService crsUserService;

    @Autowired
    private CrsUserExcelUtil crsUserExcelUtil;

/**
 * 查询CRS列表
 */
@PreAuthorize("@ss.hasPermi('bizsys:crsUser:list')")
@GetMapping("/list")
        public TableDataInfo list(CrsUser crsUser) {
        startPage();
        List<CrsUser> list = crsUserService.selectCrsUserList(crsUser);
        return getDataTable(list);
    }
    
    /**
     * 导出CRS列表
     */
    @PreAuthorize("@ss.hasPermi('bizsys:crsUser:export')")
    @Log(title = "CRS", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CrsUser crsUser) {
        List<CrsUser> list = crsUserService.selectCrsUserList(crsUser);
        ExcelUtil<CrsUser> util = new ExcelUtil<CrsUser>(CrsUser.class);
        return util.exportExcel(list, "crsUser");
    }

    /**
     * 获取CRS详细信息
     */
    @PreAuthorize("@ss.hasPermi('bizsys:crsUser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(crsUserService.selectCrsUserById(id));
    }

    /**
     * 新增CRS
     */
    @PreAuthorize("@ss.hasPermi('bizsys:crsUser:add')")
    @Log(title = "CRS", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrsUser crsUser) {
        return toAjax(crsUserService.insertCrsUser(crsUser));
    }

    /**
     * 修改CRS
     */
    @PreAuthorize("@ss.hasPermi('bizsys:crsUser:edit')")
    @Log(title = "CRS", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrsUser crsUser) {
        return toAjax(crsUserService.updateCrsUser(crsUser));
    }

    /**
     * 删除CRS
     */
    @PreAuthorize("@ss.hasPermi('bizsys:crsUser:remove')")
    @Log(title = "CRS", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(crsUserService.deleteCrsUserByIds(ids));
    }

    /**
     * 导入名单excel文件
     */
    @PostMapping("/import")
    public AjaxResult excelImport(MultipartFile file){
        List<CrsUser> crsUserList = new ArrayList<>();
        try {
            crsUserList= crsUserExcelUtil.readExcel(crsUserExcelUtil.M2F(file));
            logger.info("-------- crsUserList size :" + crsUserList.size());
            crsUserService.insertCrsUserList(crsUserList);
        } catch (Exception e) {
            logger.error("-----文件解析异常：",e);
        }
        return AjaxResult.success();
    }
}
